package cc.htdf.gwc.radar.repository.impl;

import cc.htdf.gwc.radar.domain.RadarGridMinutesData;
import cc.htdf.gwc.radar.repository.ForecastRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BatchPreparedStatementSetter;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.stereotype.Repository;

import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.List;

/**
 * Created by JT on 2017/11/13.
 */
@Repository
public class ForecastRepositoryImpl implements ForecastRepository {

    @Autowired
    private NamedParameterJdbcTemplate namedParameterJdbcTemplate;

    @Autowired
    private JdbcTemplate jdbcTemplate;

    private static final String TABLE_NAME_DATA_RADAR_GRID_MINUTES = "DATA_RADAR_GRID_MINUTES";


    @Override
    public void batchSaveGridMinutesData(List<RadarGridMinutesData> gridMinutesDataList) {
        String sql = String.format("INSERT INTO %s(STATION_ID, LAT, LON, GENTIME, VALIDTIME, QOP10MINUTES) VALUES(?, ?, ?, ?, ?, ?)",
                TABLE_NAME_DATA_RADAR_GRID_MINUTES);

        BatchPreparedStatementSetter params = new BatchPreparedStatementSetter() {
            @Override
            public void setValues(PreparedStatement ps, int i) throws SQLException {
                RadarGridMinutesData gridMinutesData = gridMinutesDataList.get(i);
                ps.setString(1, gridMinutesData.getStationId());
                ps.setString(2, gridMinutesData.getLat());
                ps.setString(3, gridMinutesData.getLon());
                ps.setString(4, gridMinutesData.getGentime());
                ps.setString(5, gridMinutesData.getValidtime());
                ps.setString(6, gridMinutesData.getQop10Minutes());
            }

            @Override
            public int getBatchSize() {
                return gridMinutesDataList.size();
            }
        };

       jdbcTemplate.batchUpdate(sql, params);
    }

}
